package com.zhxh.nzpos.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.zhxh.nzpos.db.DBHelper;
import com.zhxh.nzpos.model.LocalInfo;
import com.zhxh.nzpos.utils.RandomStringUtils;

/**
 * LocalInfo DAO
 * 
 * @author Administrator
 * 
 */
public class LocalInfoDao {

	private final static String LOG_TAG = "LocalInfoDao";
	DBHelper dbHelper;

	// private Context context;

	protected static final String TBL_NAME = "localinfo";

	public LocalInfoDao(Context context) {
		// this.context = context;
		dbHelper = new DBHelper(context);
	}

	/**
	 * add new localInfo
	 * 
	 * @param localInfo
	 */
	public String save(LocalInfo localInfo) {
		Log.i(LOG_TAG, "LocalInfoDao  save " + localInfo.toString());
		localInfo.setId(RandomStringUtils.randomNumeric(10));
		long time = System.currentTimeMillis();
		localInfo.setCreated(time);
		localInfo.setModified(time);

		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.beginTransaction();// 开始事务
		try {
			db.execSQL(
					"insert into localinfo(POSID,CardID,Password,Name,ServerIP,ServerPort,created,modified) values(?,?,?,?,?,?,?,?)",
					new Object[] { localInfo.getPostId(),
							localInfo.getCardID(), localInfo.getName(),
							localInfo.getServerIP(), localInfo.getServerPort(),
							localInfo.getCreated(), localInfo.getModified() });

			db.setTransactionSuccessful();
		} catch (Exception e) {
			Log.i("LocalInfoDao  save", e.getMessage());
		}
		db.endTransaction();// 事务结束;
		dbHelper.close();
		return localInfo.getId();
	}

	/**
	 * update a localInfo
	 * 
	 * @param localInfo
	 */

	public void update(LocalInfo localInfo) {
		Log.i(LOG_TAG, "LocalInfoDao  update " + localInfo.toString());
		localInfo.setModified(System.currentTimeMillis());
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.beginTransaction();// 开始事务

		try {
			db.execSQL(
					"update localinfo set POSID=?,CardID=?,Password=?,Name=?,ServerIP=?,ServerPort=?,modified=? where ID=?",
					new Object[] { localInfo.getPostId(),
							localInfo.getCardID(), localInfo.getName(),
							localInfo.getServerIP(), localInfo.getServerPort(),
							localInfo.getModified(), localInfo.getId() });

			// db.setTransactionSuccessful();
		} catch (Exception e) {
			Log.i("LocalInfoDao  update", e.getMessage());
		}
		db.endTransaction();// 事务结束;
		dbHelper.close();
	}

	/**
	 * get a localInfo by id
	 * 
	 * @param i
	 * @return
	 */
	public LocalInfo find(String id) {
		Log.i(LOG_TAG, "LocalInfoDao  find " + id);
		LocalInfo localInfo = null;
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db
				.rawQuery(
						"select ID,POSID,CardID,Password,Name,ServerIP,ServerPort,created,modified  from localinfo where ID=?",
						new String[] { id });
		if (cursor.moveToNext()) {
			localInfo = new LocalInfo();

			localInfo.setId(cursor.getString(cursor.getColumnIndex("ID")));
			localInfo.setPostId(cursor.getString(1));
			localInfo.setCardID(cursor.getString(2));
			localInfo.setPassword(cursor.getString(3));
			localInfo.setName(cursor.getString(4));
			localInfo.setServerIP(cursor.getString(5));
			localInfo.setServerPort(cursor.getString(6));
			localInfo.setCreated(cursor.getLong(7));
			localInfo.setModified(cursor.getLong(8));
			
		}
		cursor.close();
		dbHelper.close();
		return localInfo;
	}

	/**
	 * delete a localInfo by id
	 * 
	 * @param id
	 */
	public void delete(String id) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.execSQL("delete from localinfo where ID=?", new Object[] { id });
		dbHelper.close();
	}

	/**
	 * delete all
	 * 
	 * @param id
	 */
	public void deleteAll() {
		Log.i(LOG_TAG, "  delete all from  localinfo: ");
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.execSQL("delete from localinfo");
		dbHelper.close();
	}

	/**
	 * fetch localInfos
	 * 
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	public List<LocalInfo> getScrollData(int pageNumber, int pageSize) {
		List<LocalInfo> localInfos = new ArrayList<LocalInfo>();
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db
				.rawQuery(
						"select ID,POSID,CardID,Password,Name,ServerIP,ServerPort,created,modified  from localinfo order by modified desc limit ?,?",
						new String[] {
								String.valueOf((pageNumber - 1) * pageSize),
								String.valueOf(pageSize) });// pageNumber页码
		while (cursor.moveToNext()) {// pageSize每页获取的记录数
			LocalInfo localInfo = new LocalInfo();
			localInfo.setId(cursor.getString(cursor.getColumnIndex("ID")));
			localInfo.setPostId(cursor.getString(1));
			localInfo.setCardID(cursor.getString(2));
			localInfo.setPassword(cursor.getString(3));
			localInfo.setName(cursor.getString(4));
			localInfo.setServerIP(cursor.getString(5));
			localInfo.setServerPort(cursor.getString(6));
			localInfo.setCreated(cursor.getLong(7));
			localInfo.setModified(cursor.getLong(8));
			localInfos.add(localInfo);
		}
		cursor.close();
		dbHelper.close();
		return localInfos;
	}

	/**
	 * fetch all localInfos
	 * 
	 * @return
	 */
	public List<LocalInfo> getAll() {
		List<LocalInfo> localInfos = new ArrayList<LocalInfo>();
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db
				.rawQuery(
						"select ID,POSID,CardID,Password,Name,ServerIP,ServerPort,created,modified  from localinfo order by modified desc",
						null);// pageNumber页码
		while (cursor.moveToNext()) {// pageSize每页获取的记录数
			LocalInfo localInfo = new LocalInfo();
			localInfo.setId(cursor.getString(cursor.getColumnIndex("ID")));
			localInfo.setPostId(cursor.getString(1));
			localInfo.setCardID(cursor.getString(2));
			localInfo.setPassword(cursor.getString(3));
			localInfo.setName(cursor.getString(4));
			localInfo.setServerIP(cursor.getString(5));
			localInfo.setServerPort(cursor.getString(6));
			localInfo.setCreated(cursor.getLong(7));
			localInfo.setModified(cursor.getLong(8));
			localInfos.add(localInfo);
		}
		cursor.close();
		dbHelper.close();
		return localInfos;
	}

	/**
	 * get localInfo's count
	 * 
	 * @return
	 */
	public long getCount() {
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		long count = DatabaseUtils.queryNumEntries(db, TBL_NAME);
		dbHelper.close();
		return count;
	}

}
